package com.xc.file.web.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.core.annotation.Authority;
import com.xc.core.bean.QueryBean;
import com.xc.core.model.TokenModel;
import com.xc.file.bean.CdnBean;
import com.xc.file.bean.FileBean;
import com.xc.file.bean.PackFileBean;
import com.xc.file.bean.UpdateFileBean;
import com.xc.file.config.Constants;
import com.xc.file.dto.CdnDto;
import com.xc.file.dto.FileDto;
import com.xc.file.dto.UrlDto;
import com.xc.file.entity.FileEntity;
import com.xc.file.service.FileService;
import com.xc.file.service.FolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>文件管理rest接口</p>
 *
 * @author xc
 * @version v1.0.0
 */
@Api(tags = "文件管理rest接口")
@RestController
public class FileRest {

    @Autowired
    private FileService fileService;
    @Autowired
    private Constants constants;
    @Autowired
    private FolderService folderService;

    @ApiOperation(value = "创建当前用户目录下的zip文件包")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录id", name = "folderId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PostMapping("/current_user/folder/{folderId}/zip_packages")
    @Authority
    public FileDto createCurrentUserFolderZipPackage(TokenModel tokenModel, @PathVariable String folderId, @RequestBody PackFileBean packFileBean) {
        if (folderId == null) {
            folderId = constants.getRootFolder();
        }
        //验证目录信息
        folderId = folderService.verifyFolder(folderId, tokenModel.getUserId());
        return folderService.packFolderFiles(tokenModel.getUserId(), folderId, packFileBean, tokenModel.getAccessToken());
    }

    @ApiOperation(value = "删除当前用户的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "fileId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @DeleteMapping(value = "/current_user/file/{fileId}")
    @Authority
    public FileEntity deleteCurrentUserFile(TokenModel tokenModel, @PathVariable String fileId) {
        return fileService.deleteUserFile(tokenModel.getUserId(), fileId);
    }

    @ApiOperation(value = "修改当前用户的文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "fileId", paramType = "path", required = true),
            @ApiImplicitParam(value = "文件名称", name = "name", paramType = "body", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PutMapping(value = "/current_user/file/{fileId}")
    @Authority
    public FileDto updateCurrentUserFile(TokenModel tokenModel, @PathVariable("fileId") String fileId, @RequestBody UpdateFileBean updateFileBean) {
        return fileService.updateUserFile(tokenModel.getUserId(), fileId, updateFileBean);
    }

    @ApiOperation(value = "获取当前用户的文件集合")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @GetMapping(value = "/current_user/files")
    @Authority
    public List<FileDto> getCurrentUserFiles(TokenModel tokenModel, @ModelAttribute QueryBean queryBean, @ModelAttribute FileBean fileBean) {
        return fileService.getUserFileList(tokenModel.getUserId(), queryBean, fileBean);
    }

    @ApiOperation(value = "创建当前用户的cdn地址")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PostMapping(value = "/current_user/cdn_url")
    @Authority
    public CdnDto createCdnUrl(TokenModel tokenModel, @RequestBody CdnBean cdnBean) {
        return fileService.createCdnUrl(tokenModel.getUserId(), cdnBean);
    }
}
